<?php
session_start();
      if (isset($_SESSION['k_username'])) {
require_once('html2fpdf.php');
// activate Output-Buffer:
ob_start();

$link=mysql_connect("localhost", "gotica_gothic", "gotica") ;
$editadopor=$_SESSION['k_username'];
$fecha = date("y-m-d h:i:s");
mysql_select_db("gotica_ordencompras" ,$link);
?>
<html>
<head>
<title>Orden</title>
<style type="text/css">
<!--
img.caja {
position:absolute;
left:0px;
top:0px;
z-index:-1;
} 
-->
</style>
</head>
<body>
<br />
<br />
<br />
<?php
$sql="SELECT * FROM ordencompra Where idorden=$_POST[idorden]";
$result = mysql_query($sql);
$row=mysql_fetch_array($result);
$total=0;
$sqlauth="SELECT * FROM Gestionador WHERE idgestionador=".$row['idgestionador'];
$resultauth= mysql_query($sqlauth);
$auth =mysql_fetch_array($resultauth);
$firma=$auth['firma'];

$src="logogotica.jpg";
?>
<table width="100%" heigth ="25%" border="0">
      <tr>
        <td width="9%">Cliente</td>
<?
$sql2="SELECT * FROM proveedor WHERE idproveedor=".$row[idproveedor];
$result2 = mysql_query($sql2);
$row2=mysql_fetch_array($result2);

echo "<td >$row2[N_proveedor]</td>"; ?>
        <td width>N Orden</td>
 <? echo "<td>$row[idorden]</td>"; ?>
<td rowspan=3><? echo "<img src=$src width=\"150\" />"; ?> </td>
      </tr>
      <tr>
        <td>Direccion:</td>
		<td colspan=3>
 <?      
echo $row2['T_direccion']; ?>
</td>
</tr>
	<tr>
        <td>Contacto:</td>
       <td><? echo $_GET[editado]; ?></td>
	<td>Contrato</td>
		<td >

      </td>   
      </tr>
      <tr>
        <td>RUC:</td>
		<td colspan=4 >
<? echo $row2['T_RUC']; ?>
        </td>
        
      </tr>
      <tr>
        <td>Medio</td>
<?   
$sql3="SELECT * FROM cliente WHERE idcliente=".$row[idcliente];
$result3 = mysql_query($sql3);
$row3=mysql_fetch_array($result3);

echo "   <td>$row3[N_Cliente]</td>"; ?>

        <td>Area</td>
<?   
$sql30="SELECT * FROM Area WHERE idarea=".$row[idarea];
$result30 = mysql_query($sql30);
$row30=mysql_fetch_array($result30);

echo "   <td colspan=2>$row30[area]</td>"; ?>
      </tr>
</table>
<br />
<br />
<br />
<p>Detalle Pedido</p>

<table align="left" width="70%" heigth ="35%" border="1">
	<tr><td>Codigo</td>  <td>Producto</td><td>Cantidad</td><td>Unidades</td></tr>
	<? 
	$sql4="SELECT * FROM detallepedido WHERE idorden=".$row['idorden'];
$result4 = mysql_query($sql4);
while ($row4=mysql_fetch_array($result4)){
	echo "<tr><td>";
$sql5="SELECT tipoproducto.siglatprod , producto.codigo, producto.N_producto , unidad.n_unidad  FROM tipoproducto , producto, unidad WHERE producto.idtipoprod=tipoproducto.idtipoprod and producto.idproducto=".$row4['idproducto']." and idunidad=".$row4['idunidad'];
$result5 = mysql_query($sql5);
$row5=mysql_fetch_array($result5);
   $codigo=$row5['siglatprod']."-".$row5['codigo']; 
	echo $codigo;
	echo "</td><td>";
	echo $row5['N_producto']; 
$sql6="SELECT preciounitario, idmoneda  FROM prod_provdo WHERE idproducto=".$row4['idproducto']." and idproveedor=".$row[idproveedor];
$result6 = mysql_query($sql6);
$row6=mysql_fetch_array($result6);
$idmoneda=$row6['idmoneda'];
$sql10="SELECT *  FROM moneda WHERE idmoneda=$idmoneda";
$result10 = mysql_query($sql10);
$row10=mysql_fetch_array($result10);
$total+=$row4['Q_cantidad']*$row6['preciounitario'];
	echo "</td>";
	echo "<td>".$row4['Q_cantidad']."</td>";
	echo "<td>".$row5['n_unidad']."</td>";
	echo "</tr>";
}

	?>
</table>
<br />
<br />
<br />
<p>Facturar a:</p>
      <table width="100%"  heigth ="38%" border="0">
        <tr>
          <td width="12%">Razon Social:</td>
  <?  echo "<td width=\"52%\">$row3[N_Cliente]</td> "; ?>
          <td width="12%">Total Neto</td>
  <?
 
echo "<td width=\"24%\"> ".$row10['tagmoneda']." ". $total."</td>";

?>
        </tr>
        <tr>
          <td>Direccion:</td>
<? echo "<td>$row3[T_direccion]</td>"; ?>
          <td>IGV</td>
	<?  $igv=$total*0.19;
	echo " <td> ".$row10['tagmoneda']." ". $igv."</td>"; ?>
        </tr>
        <tr>
          <td>RUC:</td>
       <? echo "<td>$row3[T_RUC]</td>"; ?>
          <td>Total</td>
		<?
		$tot=$total+$igv;
		echo "<td> ".$row10['tagmoneda']." ".$tot."</td>"; ?>
        </tr>
        <tr>
          <td colspan=4>Observaciones:</td></tr>
		<tr>  
    <? echo "<td colspan=4 >$row[Observaciones]</td>"; ?>

        </tr>
 </table> 
<? 
if ($row['idestado']==2){
echo "<table><tr align=\"center\"><td>";
 echo " <img src=$firma  />"; 
echo "</td></tr><tr align=\"center\"><td >___________________________</td></tr>";
echo "<tr align=\"center\" ><td>Firma Autorizacion</td>";
  echo "</tr></table>"; }?>


</body>
</html>
<?
// Output-Buffer in variable:
$html=ob_get_contents();
// delete Output-Buffer
ob_end_clean();
$pdf = new HTML2FPDF();
$or="orden".$row['idorden'].".pdf";
$pdf->DisplayPreferences('HideWindowUI');
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->Output($or,'D');
?>

<?
}else
{
	echo "<SCRIPT LANGUAGE=\"javascript\">";
echo "location.href = \"index.php\"";
echo "</SCRIPT>";
	}
?>